<?php
include dirname(dirname(__FILE__)).'/admin-verify.php';
include dirname(__FILE__).'/functions.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';
if(isset($_POST['id']))$row = getUserById(addslashes($_POST['id']));
else if(isset($_GET['id']))$row = getUserById($_GET['id']);
if(empty($row)) {
	alertInfo('用户不存在',getHost().'/admin/user-manage/index.php');
	exit();
}
?>


<!DOCTYPE html>
<html>
	<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php' ?>
		


		<?php 
				if(isset($_POST['submited'])&&$_POST['submited']==1) {
					$id = addslashes($_POST['id']);
					$username = addslashes($_POST['username']);
					if(isset($_POST['password'])&&$_POST['password']!='')$password = md5(addslashes($_POST['password']));
					else $password = $row['password'];
					$student_id = addslashes($_POST['student_id']);
					$department = addslashes($_POST['department']);
					$phone = addslashes($_POST['phone']);
					$user_real_name = addslashes($_POST['user_real_name']);
					$sex = addslashes($_POST['sex']);
					$grade = addslashes($_POST['grade']);
					$user_role = addslashes($_POST['user_role']);
					$is_user_on_duty = 0;
					if(isset($_POST['is_user_on_duty'])&&$_POST['is_user_on_duty']==1)$is_user_on_duty = 1;
					if($username==null||$user_role==null) {
						alertInfo("请先完成必填项！",getHost().'/admin/user-manage/edit-user.php?id='.$id);
						exit();
					}
					if($row['username']!=$username) {
						alertInfo("无效的参数!",getHost().'/admin/user-manage/edit-user.php?id='.$id);
						exit();
					}
					if($user_role=='admin'&&$_SESSION['user_role']!='super_admin') {
						alertInfo("你没有该操作权限！",getHost().'/admin/user-manage/edit-user.php?id='.$id);
						exit();
					}
					$params = array('username' => '"'.$username.'"',
					'password' => '"'.$password.'"',
					'student_id' => '"'.$student_id.'"',
					'sex' => '"'.$sex.'"',
					'grade' => '"'.$grade.'"',
					'department' => '"'.$department.'"',
					'user_role' => '"'.$user_role.'"',
					'phone' => '"'.$phone.'"',
					'user_real_name' => '"'.$user_real_name.'"',
					'is_user_on_duty' => $is_user_on_duty);
					if(updateUser($params,$id)) {
						echo '<script type="text/javascript">alert("修改用户信息成功！");window.location="'.
						getHost().'/admin/user-manage/index.php"</script>';
						exit();
					} else {
						echo '<script type="text/javascript">alert("修改用户信息成功！");</script>';
						exit();
					}
				}


			?>


		<script type="text/javascript">
			$(document).ready(function () {
				$("input").attr("maxlength","16");
				$(".required").blur(function () {
				var item = $.trim($(this).val());
				if(item==null||item=='') {
					$(this).parent().parent().addClass("has-error");
					$(this).next().fadeIn();
					$("#edit-user-btn").attr("disabled","disabled");
				} else {
					$(this).parent().parent().removeClass("has-error");
					$(this).next().hide();
					$("#edit-user-btn").removeAttr("disabled");
				}
				});
				$("#password").blur(function() {
					var _password = $.trim($(this).val());
					if(_password.length>0&&_password.length<4) {
						$("#password").parent().parent().addClass("has-error");
						$("#password").next().html("密码长度应为4~16位").fadeIn();
						$("#edit-user-btn").attr("disabled","disabled");
					} else {
						$("#password").parent().parent().removeClass("has-error");
						$("#password").next().hide();
						$("#edit-user-btn").removeAttr("disabled");
					}
				});
				$("#edit-user-btn").click(function() {
					if($("#username").val()==''
						||$("#user_role").val()=='') {
						alert("请完成必填项!");
						$("#edit-user-btn").attr("disabled","disabled");
						return false;
					}
					return true;
				});
				$("#edit-user-cancel-btn").click(function () {
					window.location= <?php echo '"'.getHost().'/admin/user-manage/index.php"'; ?>;
					return false;
				});
				<?php 
				if($row['user_role']!=null) echo '
					$("#user_role option[value=\''.$row['user_role'].'\']").attr("selected",true);
					';
				if($row['is_user_on_duty']==1)echo '
				$("#is_user_on_duty").attr("checked","checked");
				'; ?>
			});
			
		</script>
		<link rel="stylesheet" type="text/css" href="../css/style.css">
	</head>
	<body>
		<div class="col-md-12">
			<div>
				<h3>修改用户 <?php echo $row['username'] ; ?> 信息</h3>
			</div>
			<div>
					<form class="form-horizontal" role="form" method="post" action="edit-user.php">
							  <div class="form-group">
							    <label for="username" class="col-sm-2 control-label">用户名</label>
							    <div class="col-sm-10">
							      <input type="text" name="username" class="form-control normal-input required" 
							      value=<?php echo '"'.$row['username'].'"' ?> id="username" placeholder="用户名" readonly>
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="password" class="col-sm-2 control-label">密码</label>
							    <div class="col-sm-10">
							      <input type="password" name="password" class="form-control normal-input" 
							      value="" id="password" placeholder="若不改变请置空">
							      <p class="help-block" style="display:none"></p>
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="student_id" class="col-sm-2 control-label">学号</label>
							    <div class="col-sm-10">
							      <input type="text" name="student_id" class="form-control normal-input"
							      value=<?php echo '"'.$row['student_id'].'"'; ?> id="student_id" placeholder="学号">
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="department" class="col-sm-2 control-label">学院</label>
							    <div class="col-sm-10">
							      <input type="text" name="department" class="form-control normal-input" 
							      value=<?php echo '"'.$row['department'].'"'; ?> id="department" placeholder="学院">
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="grade" class="col-sm-2 control-label">年级</label>
							    <div class="col-sm-10">
							      <input type="text" name="grade" class="form-control normal-input"
							      value=<?php echo '"'.$row['grade'].'"'; ?> id="grade" placeholder="年级">
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="user_real_name" class="col-sm-2 control-label">姓名</label>
							    <div class="col-sm-10">
							      <input type="text" name="user_real_name" class="form-control normal-input"
							      value=<?php echo '"'.$row['user_real_name'].'"'; ?> id="user_real_name" placeholder="姓名">
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="sex" class="col-sm-2 control-label">性别</label>
							    <div class="col-sm-10">
							      <input type="text" name="sex" class="form-control normal-input" 
							      value=<?php echo '"'.$row['sex'].'"'; ?> id="sex" placeholder="性别">
							    </div>
							  </div>
							  <div class="form-group">
							    <label for="phone" class="col-sm-2 control-label">联系方式</label>
							    <div class="col-sm-10">
							      <input type="text" name="phone" class="form-control normal-input" 
							      value=<?php echo '"'.$row['phone'].'"'; ?> id="phone" placeholder="手机或电话">
							    </div>
							  </div>
							  
							  <div class="form-group">
							    <label for="user_role" class="col-sm-2 control-label">用户类型</label>
							    <div class="col-sm-10">
							      <select class="form-control normal-input required" id="user_role" name="user_role">
							      <option value=""></option>
							      <option value="user">普通用户</option>
							      <?php 
							      		if($_SESSION['user_role']=='super_admin')
							      			echo '<option value="admin">管理员</option>';
							      ?>
								  </select>
								  <span class="help-block" style="display:none">此项不能为空</span>
							    </div>
							  </div>
							  <div class="form-group">
							  <label for="is_user_on_duty" class="col-sm-2 control-label" >是否需要值班</label>
							    <div class="col-sm-10">
							      <div class="checkbox">
							        <label>
							          <input type="checkbox" id="is_user_on_duty" name="is_user_on_duty" value="1">需要值班
							        </label>
							      </div>
							    </div>
							  </div>
							  <div class="form-group">
							    <div class="col-sm-offset-2 col-sm-10">
							      <button type="submit" class="btn btn-primary" id="edit-user-btn">修改用户信息</button>
							    	<button class="btn btn-default" id="edit-user-cancel-btn">取消</button>
							    </div>
							  </div>
							  <input type="hidden" name="submited" value="1">
							  <input type="hidden" name="id" value=<?php echo '"'.$row['id'].'"'; ?>> 
							</form>
				</div>
			</div>
	</body>
</html>
